package com.plate.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plate.common.datasource.base.MyBaseServiceImpl;
import com.plate.modules.system.domain.bo.SysUserQueryBO;
import com.plate.modules.system.domain.po.SysUser;
import com.plate.modules.system.domain.po.SysUserRole;
import com.plate.modules.system.domain.vo.SysRoleVO;
import com.plate.modules.system.domain.vo.SysUserVO;
import com.plate.modules.system.mapper.SysUserMapper;
import com.plate.modules.system.service.ISysRoleService;
import com.plate.modules.system.service.ISysUserRoleService;
import com.plate.modules.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName SysUserServiceImpl
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年10月22日 13:46
 * @Version 1.0
 */

@Service
public class SysUserServiceImpl extends MyBaseServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public SysUserVO getUserInfoById(String userId) {
        SysUserVO userVO = new SysUserVO();
        // 用户信息
        SysUser sysUser = this.getById(userId);
        BeanUtils.copyProperties(sysUser, userVO);
        // 角色信息
        List<SysRoleVO> roleList = sysRoleService.getRoleListByUserId(userId);
        userVO.setRoles(roleList);
        return userVO;
    }

    @Override
    public Page<SysUser> getBindUserLit(SysUserQueryBO queryBO) {
        List<Long> userIdList = sysUserRoleService.listByRoleId(queryBO.getRoleId());
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysUser::getId, userIdList);
        if (!StrUtil.isBlankIfStr(queryBO.getUsername())) {
            wrapper.like(SysUser::getUsername, queryBO.getUsername());
        }
        Page<SysUser> page = new Page<>(queryBO.getPage(), queryBO.getSize());
        Page<SysUser> userPage = this.page(page, wrapper);
        return userPage;
    }

    @Override
    public Page<SysUser> getUnBindUserLit(SysUserQueryBO queryBO) {
        List<Long> userIdList = sysUserRoleService.listByRoleId(queryBO.getRoleId());
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.notIn(SysUser::getId, userIdList);
        if (!StrUtil.isBlankIfStr(queryBO.getUsername())) {
            wrapper.like(SysUser::getUsername, queryBO.getUsername());
        }
        Page<SysUser> page = new Page<>(queryBO.getPage(), queryBO.getSize());
        Page<SysUser> userPage = this.page(page, wrapper);
        return userPage;
    }

}
